﻿using System;
using System.IO;
using System.Collections.Generic;
using Google.Documents;
using Google.Contacts;
using System.Linq;
using System.Text;
using DataType;
using Google.GData.AccessControl;
using Utilities;
using UILibrary;

namespace MyGDocsAddIn.Proxy
{
    public class UIProxy : MarshalByRefObject
    {
        private static UIProxy m_UIProxy = new UIProxy();

        /// <summary>
        /// The server application will use this singleton to set this type public 
        /// for remoting
        /// </summary>
        internal static UIProxy Instance
        {
            get { return m_UIProxy; }
        }

        private UIProxy() { }

        /// <summary>
        /// The client application will work with an instance that will be 
        /// retrieved thru remoting
        /// </summary>
        /// <returns></returns>
        public static UIProxy GetRemoteInstance()
        {
            object obj = Activator.GetObject(typeof(UIProxy), "tcp://localhost:9999/UIProxy");
            return obj as UIProxy;
        }

        public void DoUILogin()
        {
            Globals.ThisAddIn.m_MyGDocsForm.Login_Click(null, null);
        }
        
        //create separate class - DataProxy
        public void DoLogin(string UserName, string Password)
        {
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoLogin(new Credentials(UserName, Password));
        }

        public void DoUploadDocument()
        {
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoSaveDocumentInCloud();
        }

        public void DoDownloadDocument(string path)
        {
            Document.DownloadType type = Document.DownloadType.xls;
            Document dt = new Document();
            dt.AtomEntry = Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.m_DocInfo.m_DocEntry;
            DownloadInfo info = new DownloadInfo(dt, type);

            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoDownloadDocument(info);
            FileInfo fileInfo = new FileInfo(path);
            FileStream stream = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            CommonOperations.DoSaveDocumentContent(stream, info.m_Stream);
            stream.Close();
        }

        public void DoUpdateDocument()
        {
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoUpdateDocument();
        }

        public long GetContactsCount()
        {
            FeedWrapper<Contact> cnt = new FeedWrapper<Contact>();
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoGetContacts(cnt);

            return cnt.m_Feed.Entries.Count();
        }

        public long GetDocumentsCount()
        {
            FeedWrapper<Document> doc = new FeedWrapper<Document>();
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoGetDocuments(doc);
            
            return doc.m_Feed.Entries.Count();
        }

        public MyGDocsForm GetUIAccessPoint()
        {
            return Globals.ThisAddIn.m_MyGDocsForm;
        }

        public long ContactItemsCount()
        {
            return Globals.ThisAddIn.m_MyGDocsForm.ContactListBox.Items.Count;
        }

        public long TreeNodesCount()
        {
            return Globals.ThisAddIn.m_MyGDocsForm.m_Tree.m_TreeView.GetNodeCount(true);
        }

        public void CheckItem(string[] itemsTitle, bool Val)
        {
            for (int i = 0; i < itemsTitle.Length; ++i)
            {
                int index = Globals.ThisAddIn.m_MyGDocsForm.ContactListBox.Items.IndexOf(itemsTitle[i]);
                if (index != -1)
                    Globals.ThisAddIn.m_MyGDocsForm.ContactListBox.SetItemChecked(index, Val);
            }
        }

        public void ShareDoc()
        {
            Globals.ThisAddIn.m_MyGDocsForm.ShareButton_Click(null, null);
        }

        public string[] GetCurrentDocumentAcl()
        {
            ShareInfoCollection info = new ShareInfoCollection(Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.m_DocInfo.m_DocEntry);
            Globals.ThisAddIn.m_MyGDocsForm.m_LocalView.DoGetDocumentAcl(info);
            
            string[] aclList = new string[info.m_Feed.Entries.Count()];

            for (int i = 0; i < aclList.Length; ++i)
            {
                AclEntry entry = info.m_Feed.Entries[i] as AclEntry;
                aclList[i] = entry.Scope.Value;
            }

            return aclList;
        }

        public void SetTab(Int32 Val)
        {
            Globals.ThisAddIn.m_MyGDocsForm.m_TS_UI.SetTab(Globals.ThisAddIn.m_MyGDocsForm.MyGDocsTabControl, Val);
        }
    }
}
